package com.gdust.lease.web.admin.controller.login;


import com.gdust.lease.common.login.LoginUserHolder;
import com.gdust.lease.common.result.Result;
import com.gdust.lease.web.admin.service.LoginService;
import com.gdust.lease.web.admin.vo.login.CaptchaVo;
import com.gdust.lease.web.admin.vo.login.LoginVo;
import com.gdust.lease.web.admin.vo.system.user.SystemUserInfoVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "后台管理系统登录管理")
@RestController
@RequestMapping("/admin")
public class LoginController {
    //注入登录管理的service
    @Autowired
    private LoginService loginService;

    /**
     * 获取图形验证码
     *
     * @return
     */
    @Operation(summary = "获取图形验证码")
    @GetMapping("login/captcha")
    public Result<CaptchaVo> getCaptcha() {
        //自定义方法:获取图形验证码
        CaptchaVo captcha = loginService.getCaptcha();
        return Result.ok(captcha);
    }

    /**
     * 登录接口
     * @param loginVo 后台管理系统登录信息VO，内含：用户名、密码、验证码key、验证码code
     * @return
     */
    @Operation(summary = "登录")
    @PostMapping("login")
    public Result<String> login(@RequestBody LoginVo loginVo) {
        //自定义方法：登录接口
        String token = loginService.login(loginVo);
        return Result.ok(token);
    }

    @Operation(summary = "获取登陆用户个人信息")
    @GetMapping("info")
    public Result<SystemUserInfoVo> info() {
        SystemUserInfoVo userInfo = loginService.getLoginUserInfo(LoginUserHolder.getLoginUser().getUserId());
        return Result.ok(userInfo);
    }
}